package com.iot.framework.rabbitmq.service;


import com.alibaba.fastjson2.JSONObject;
import com.iot.common.enums.*;
import com.iot.common.utils.DateUtils;
import com.iot.system.domain.DeviceDetails;
import com.iot.system.domain.DeviceState;
import com.iot.system.domain.DeviceStateLogs;
import com.iot.system.service.IDeviceDetailsService;
import com.iot.system.service.IDeviceStateLogsService;
import com.iot.system.service.IDeviceStateService;
import com.iot.system.vo.DeviceOperationVo;
import com.iot.system.vo.DeviceStateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Component
@Slf4j
public class DeviceOperationService {

    @Resource
    private IDeviceDetailsService iDeviceDetailsService;

    @Resource
    private IDeviceStateService iDeviceStateService;

    @Resource
    private IDeviceStateLogsService iDeviceStateLogsService;



    //判断消息类型 当前消息属于设备状态  分为设备上线 设备下线
    public void deviceOperation(String message) {
        JSONObject jsonObject = JSONObject.parseObject(message);
        log.info("DeviceStateService_deviceState_设备运行状态转换完成:{}",jsonObject);
        DeviceOperationVo deviceOperationVo = jsonObject.toJavaObject(DeviceOperationVo.class);
        //获取设备详情
        DeviceDetails deviceDetails = iDeviceDetailsService.selectDeviceDetailsByToken(deviceOperationVo.getCredentials());
        if(Objects.isNull(deviceDetails)){
            return;
        }
        processingDeviceState(deviceOperationVo, deviceDetails);
        //设备状态更新记录
        processingDeviceStateLogs(deviceOperationVo, deviceDetails);
    }

    private void processingDeviceStateLogs(DeviceOperationVo deviceOperationVo, DeviceDetails deviceDetails) {
        if (Objects.equals(deviceOperationVo.getDeviceStart(), DeviceOperationType.OPERATION.getNumber())) {
            DeviceStateLogs deviceStateLogs = new DeviceStateLogs();
            deviceStateLogs.setDeviceId(deviceDetails.getId());
            deviceStateLogs.setDeviceBootUp(new Date());
            deviceStateLogs.setDeviceState(DeviceStatusRecordType.OPERATION.getNumber());
            deviceStateLogs.setCreateTime(new Date());
            deviceStateLogs.setUpdateTime(new Date());
            deviceStateLogs.setCreateBy("10101010");
            deviceStateLogs.setUpdateBy("10101010");
            deviceStateLogs.setIsDeleted(0);
            iDeviceStateLogsService.insertDeviceStateLogs(deviceStateLogs);
        }

        if (Objects.equals(deviceOperationVo.getDeviceStart(), DeviceOperationType.STOP_OPERATION.getNumber())) {
            //如果是报警解除 则查询最近一次的报警
            DeviceStateLogs deviceStateLogs = iDeviceStateLogsService.selectDeviceStateLogsByDeviceId(deviceDetails.getId(),DeviceStatusRecordType.OPERATION.getNumber());
            if (Objects.isNull(deviceStateLogs)||Objects.isNull(deviceStateLogs.getId())) {
                return;
            }
            deviceStateLogs.setDeviceShutDown(new Date());
            iDeviceStateLogsService.updateDeviceStateLogs(deviceStateLogs);
        }
    }

    //设备状态处理
    private void processingDeviceState(DeviceOperationVo deviceOperationVo, DeviceDetails deviceDetails) {
        log.info("DeviceStateService_processingDeviceState_设备运行状态处理开始:{}",deviceOperationVo);
        DeviceState deviceState = iDeviceStateService.getDeviceStateByDeviceId(deviceDetails.getId());
        if (Objects.isNull(deviceState.getId())) {
            log.info("DeviceStateService_processingDeviceState_当前设备不存在状态，所以无法将开机状态改为报警状态");
            return;
        }

        log.info("DeviceStateService_processingDeviceState_设备状态不为空处理开始:{}",deviceOperationVo);
        if (Objects.nonNull(deviceState.getId())) {
            boolean currentState = Objects.equals(deviceState.getStateType(), DeviceStatusType.BOOT_UP.getNumber()) ||
                    Objects.equals(deviceState.getStateType(), DeviceStatusType.DEVICE_RUN_STOP.getNumber()) ||
                    Objects.equals(deviceState.getStateType(), DeviceStatusType.FAILURE.getNumber()) ||
                    Objects.equals(deviceState.getStateType(), DeviceStatusType.STOP_FAILURE.getNumber());
            if (currentState&&Objects.equals(deviceOperationVo.getDeviceStart(), DeviceOperationType.OPERATION.getNumber())) {
                deviceState.setStateType(DeviceStatusType.DEVICE_RUN.getNumber());
                deviceState.setStateTime(new Date());
                iDeviceStateService.updateDeviceState(deviceState);
            }
            if (Objects.equals(deviceState.getStateType(), DeviceStatusType.DEVICE_RUN.getNumber())&&Objects.equals(deviceOperationVo.getDeviceStart(), DeviceOperationType.STOP_OPERATION.getNumber())) {
                deviceState.setStateType(DeviceStatusType.DEVICE_RUN_STOP.getNumber());
                deviceState.setStateTime(new Date());
                iDeviceStateService.updateDeviceState(deviceState);
            }
            log.info("DeviceStateService_processingDeviceState_设备状态不为空处理结束:{}",deviceState);
            return;
        }
    }

}
